home *** CD-ROM | disk | FTP | other *** search
/ MPEG Toolkit / MPEG Toolkit.iso / os2 / mpegplay / xstub.c < prev    next >
C/C++ Source or Header  |  1997-01-01  |  9KB  |  403 lines

  1. /*
  2.  
  3. xstub.c - Stub for XWindows
  4.  
  5. @@@ Andy Key
  6.  
  7. */
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <stdarg.h>
  12. #define    XSTUB_C
  13. #include "xstub.h"
  14.  
  15. /*...simplementation:0:*/
  16. #define INCL_BASE
  17. #include <os2.h>
  18.  
  19. #define    SCN_W 320
  20. #define SCN_H 200
  21. static VIOMODEINFO vmiOld;
  22. static unsigned char *ptr;
  23. static int scn_w, scn_h;
  24. static unsigned char palette[0x100][3];
  25. static int npalette;
  26. static int data_depth;
  27.  
  28. /*...ssetpalette:0:*/
  29. static void setpalette(BYTE *palette, int first, int num)
  30.     {
  31.     BYTE *buf = (BYTE *) palette;
  32.     BYTE *_Seg16 buf1616 = (BYTE *_Seg16) buf;
  33.     VIOCOLORREG viocreg;
  34.     viocreg.cb = sizeof(VIOCOLORREG);
  35.     viocreg.type = 3; /* Apparently no #define for this */
  36.     viocreg.firstcolorreg = first;
  37.     viocreg.numcolorregs = num;
  38.  
  39.     /* Note: viocreg.colorregaddr should be of type CHAR *_Seg16
  40.        But it isn't! Hence if I say viocreg.colorregaddr = buf,
  41.        then the correct thunking will not be applied! */
  42.     memcpy(&viocreg.colorregaddr, &buf1616, 4);
  43.  
  44.     VioSetState(&viocreg, (HVIO) 0);
  45.     }
  46. /*...e*/
  47. /*...sscn_init:0:*/
  48. static void scn_init(void)
  49.     {
  50.     VIOMODEINFO vmi;
  51.     VIOPHYSBUF phys;
  52.     VIOOVERSCAN vioos;
  53.     unsigned char *_Seg16 ptr1616;
  54.     unsigned char status;
  55.  
  56.     vmi.cb     = 12;
  57.     vmi.fbType = 3;
  58.     vmi.color  = 8;
  59.     vmi.col    = 40;
  60.     vmi.row    = 25;
  61.     vmi.hres   = SCN_W;
  62.     vmi.vres   = SCN_H;
  63.     VioGetMode(&vmiOld, (HVIO) 0);
  64.     if ( VioSetMode(&vmi, (HVIO) 0) )
  65.         {
  66.         fprintf(stderr, "Unable to enter 320x200 at 8pp graphics mode\n");
  67.         exit(1);
  68.         }
  69.     phys.cb = 0x10000;
  70.     phys.pBuf = (unsigned char *) 0xa0000;
  71.     if ( VioGetPhysBuf(&phys, 0) )
  72.         {
  73.         fprintf(stderr, "Unable to access to physical graphics screen\n");
  74.         exit(1);
  75.         }
  76.     ptr1616 = (unsigned char *_Seg16) ( phys.asel[0] << 16 );
  77.     ptr = (unsigned char *) ptr1616;
  78.     VioScrLock(VP_WAIT, &status, (HVIO) 0);
  79.  
  80.     /* Set all palette entrys to black */
  81.     memset(palette, 0, sizeof(palette));
  82.     /* Ensure dish them out from 0 onwards */
  83.     npalette = 0;
  84.  
  85.     setpalette((BYTE *) palette, 0, 0x100);
  86.  
  87.     /* Set screen to colour 255, which will get set to black */
  88.     memset(ptr, 255, SCN_W*SCN_H);
  89.  
  90.     /* Set border colour to colour 255 */
  91.     vioos.cb = sizeof(VIOOVERSCAN);
  92.     vioos.type = 1; /* Apparently no #define for this */
  93.     vioos.color = 255;
  94.     VioSetState(&vioos, (HVIO) 0);
  95.     }
  96. /*...e*/
  97. /*...sscn_term:0:*/
  98. static void scn_term(void)
  99.     {
  100.     VioScrUnLock((HVIO) 0);
  101.     VioSetMode(&vmiOld, (HVIO) 0);
  102.     }
  103. /*...e*/
  104. /*...sscn_setdatadepth:0:*/
  105. static void scn_setdatadepth(int depth)
  106.     {
  107.     data_depth = depth;
  108.     }
  109. /*...e*/
  110. /*...sscn_setsize:0:*/
  111. static void scn_setsize(int w, int h)
  112.     {
  113.     scn_w = w;
  114.     scn_h = h;
  115.     }
  116. /*...e*/
  117. /*...sscn_setpalette:0:*/
  118. static int scn_setpalette(
  119.     unsigned char r, unsigned char g, unsigned char b
  120.     )
  121.     {
  122.     palette[npalette][0] = r;
  123.     palette[npalette][1] = g;
  124.     palette[npalette][2] = b;
  125.     return npalette++;
  126.     }
  127. /*...e*/
  128. /*...sscn_putimage:0:*/
  129. /*
  130. We are being asked to transfer a w*h image to a SCN_W*SCN_H screen.
  131. If image is larger than the screen, then we must clip it.
  132. Also, if image is smaller than screen, then we will centralise it.
  133. */
  134.  
  135. static int nopalette = 1;
  136. /*...sscn_putimage8:0:*/
  137. static void scn_putimage8(int w, int h, unsigned char *data)
  138.     {
  139.     int stride = w, xi, yi, y;
  140.     unsigned char *scn;
  141.     if ( nopalette )
  142.         {
  143.         setpalette((BYTE *) palette, 0, 0x100);
  144.         nopalette = 0;
  145.         }
  146.     if ( w > SCN_W )
  147.         {
  148.         int extra = w - SCN_W;
  149.         data += extra/2;
  150.         w = SCN_W;
  151.         }
  152.     if ( h > SCN_H )
  153.         {
  154.         int extra = h - SCN_H;
  155.         data += ((extra/2) * stride);
  156.         h = SCN_H;
  157.         }
  158.     xi = (SCN_W-w)/2;
  159.     yi = (SCN_H-h)/2;
  160.     scn = ptr + ((yi*SCN_W) + xi);
  161.     for ( y = 0; y < h; y++, data += stride, scn += SCN_W )
  162.         memcpy(scn, data, w);
  163.     }
  164. /*...e*/
  165. /*...sscn_putimage1:0:*/
  166. /*...sbitexpand:0:*/
  167. static void bitexpand(
  168.     unsigned char *scn,
  169.     unsigned char *data,
  170.     int bitindex,
  171.     int w
  172.     )
  173.     {
  174.     int x;
  175.     for ( x = 0; x < w; x++, bitindex++ )
  176.         *scn++ = ( (data[bitindex>>3]&(0x80>>(bitindex&7))) != 0 ) ? 1 : 0;
  177.     }
  178. /*...e*/
  179.  
  180. static void scn_putimage1(int w, int h, unsigned char *data)
  181.     {
  182.     int bitindex = 0;
  183.     int stride = w, xi, yi, y;
  184.     unsigned char *scn;
  185.     if ( nopalette )
  186.         {
  187.         palette[1][0] = 63;
  188.         palette[1][1] = 63;
  189.         palette[1][2] = 63;
  190.         npalette = 2;
  191.         setpalette((BYTE *) palette, 0, 0x100);
  192.         nopalette = 0;
  193.         }
  194.     if ( w > SCN_W )
  195.         {
  196.         int extra = w - SCN_W;
  197.         bitindex += extra/2;
  198.         w = SCN_W;
  199.         }
  200.     if ( h > SCN_H )
  201.         {
  202.         int extra = h - SCN_H;
  203.         bitindex += ((extra/2) * stride);
  204.         h = SCN_H;
  205.         }
  206.     xi = (SCN_W-w)/2;
  207.     yi = (SCN_H-h)/2;
  208.     scn = ptr + ((yi*SCN_W) + xi);
  209.     for ( y = 0; y < h; y++, bitindex += stride, scn += SCN_W )
  210.         bitexpand(scn, data, bitindex, w);
  211.     }
  212. /*...e*/
  213.  
  214. static void scn_putimage(int w, int h, unsigned char *data)
  215.     {
  216.     /* Sometimes we decode more than the size of the 'screen' */
  217.     if ( h > scn_h ) h = scn_h;
  218.     switch ( data_depth )
  219.         {
  220.         case 8:        scn_putimage8(w, h, data);    break;
  221.         case 1:        scn_putimage1(w, h, data);    break;
  222.         }
  223.     }
  224. /*...e*/
  225. /*...e*/
  226. /*...sx calls:0:*/
  227. /*...slswap:0:*/
  228. unsigned long lswap(unsigned long l)
  229.     {
  230.     return ( ( (l&0xff000000) >> 24 ) |
  231.          ( (l&0x00ff0000) >>  8 ) |
  232.          ( (l&0x0000ff00) <<  8 ) |
  233.          ( (l&0x000000ff) << 24 ) );
  234.     }
  235. /*...e*/
  236. /*...sXSetErrorHandler:0:*/
  237. void XSetErrorHandler(
  238.     int (*handler)(Display *dpy, XErrorEvent *event)
  239.     ) { /* Ignore */ }
  240. /*...e*/
  241. /*...sXOpenDisplay:0:*/
  242. static Display d = (Display) 0;
  243.  
  244. Display *XOpenDisplay(char *name)
  245.     {
  246.     scn_init();
  247.     return &d;
  248.     }
  249. /*...e*/
  250. /*...sXFlush:0:*/
  251. void XFlush(Display *dpy) { /* Ignore */ }
  252. /*...e*/
  253. /*...sXMatchVisualInfo:0:*/
  254. int XMatchVisualInfo(
  255.     Display *dpy, int screen, int bpp,
  256.     int type,
  257.     XVisualInfo *vinfo
  258.     )
  259.     {
  260.     return 1; /* Did it! */
  261.     }
  262. /*...e*/
  263. /*...sXAllocColor:0:*/
  264. int XAllocColor(Display *display, Colormap cmap, XColor *xcolor)
  265.     {
  266.     xcolor->pixel = scn_setpalette(
  267.         xcolor->red>>10, xcolor->green>>10, xcolor->blue>>10);
  268.     return 1;
  269.     }
  270. /*...e*/
  271. /*...sXFreeColors:0:*/
  272. void XFreeColors(
  273.     Display *display,
  274.     Colormap cmap,
  275.     long *pixel,
  276.     int one,
  277.     int zero
  278.     )
  279.     {
  280.     }
  281. /*...e*/
  282. /*...sXDefaultColormap:0:*/
  283. Colormap XDefaultColormap(Display *dpy, int screen)
  284.     {
  285.     /* Write me! */
  286.     }
  287. /*...e*/
  288. /*...sXCreateColormap:0:*/
  289. Colormap XCreateColormap(
  290.     Display *display, Window window,
  291.     Visual *visual, int flags
  292.     )
  293.     {
  294.     return (Colormap) 0;
  295.     }
  296. /*...e*/
  297. /*...sXSetWindowColormap:0:*/
  298. void XSetWindowColormap(Display *display, Window window, Colormap cmap)
  299.     {
  300.     }
  301. /*...e*/
  302. /*...sXCreateGC:0:*/
  303. GC XCreateGC(
  304.     Display *display, Window window,
  305.     unknown_t flags, XGCValues *xgcv
  306.     )
  307.     {
  308.     return (GC) 0;
  309.     }
  310. /*...e*/
  311. /*...sXCreateImage:0:*/
  312. /* bpp is 8 or 32 */
  313. /* flag1 is ZPixmap (usually) or XYBitmap (when mono) */
  314.  
  315. XImage *XCreateImage(
  316.     Display *display, Visual *visual,
  317.     int depth, int flag1, int flag2, char *dummy,
  318.     int w, int h, int bpp, int zero
  319.     )
  320.     {
  321.     XImage *xi;
  322.  
  323.     if ( (xi = malloc(sizeof(XImage))) == NULL )
  324.         return (XImage *) 0;
  325.     if ( (xi->data = malloc(w * h * depth/8)) == NULL )
  326.         { free(xi); return (XImage *) 0; }
  327.     memset(xi->data, 0, w*h);
  328.     xi->width  = w;
  329.     xi->height = h;
  330.     xi->bitmap_bit_order = MSBFirst;
  331.     xi->byte_order = MSBFirst;
  332.     scn_setdatadepth(depth);
  333.     return xi;
  334.     }
  335. /*...e*/
  336. /*...sXGetVisualInfo:0:*/
  337. XVisualInfo *XGetVisualInfo(
  338.     Display *dpy,
  339.     int visualclassmask, XVisualInfo *vinfo, int *numitems
  340.     )
  341.     {
  342.     *numitems = 0;
  343.     return (XVisualInfo *) 0;
  344.     }
  345. /*...e*/
  346. /*...sXCreateSimpleWindow:0:*/
  347. Window XCreateSimpleWindow(
  348.     Display *dpy,
  349.     Window root,
  350.     int x, int y, int w, int h, int n,
  351.     unsigned int fg, unsigned int bg
  352.     )
  353.     {
  354.     }
  355. /*...e*/
  356. /*...sXCreateWindow:0:*/
  357. Window XCreateWindow(
  358.     Display *dpy,
  359.     Window window,
  360.     int x, int y, int w, int h,
  361.     int one, int depth,
  362.     unsigned int class,
  363.     Visual *visual,
  364.     unsigned int mask,
  365.     XSetWindowAttributes *wswa
  366.     )
  367.     {
  368.     return (Window) 0;
  369.     }
  370. /*...e*/
  371. /*...sXResizeWindow:0:*/
  372. void XResizeWindow(Display *dpy, Window window, int w, int h)
  373.     {
  374.     scn_setsize(w, h);
  375.     }
  376. /*...e*/
  377. /*...sXGetWindowAttributes:0:*/
  378. void XGetWindowAttributes(
  379.     Display *display, Window window,
  380.     XWindowAttributes *xwa
  381.     )
  382.     {
  383.     }
  384. /*...e*/
  385. /*...sXFree:0:*/
  386. /* Used to free result from XGetVisualInfo */
  387. void XFree(void *ptr)
  388.     {
  389.     }
  390. /*...e*/
  391. /*...sXPutImage:0:*/
  392. void XPutImage(
  393.     Display *display, Window window, GC gc,
  394.     XImage *ximage,
  395.     int zero1, int zero2, int zero3, int zero4,
  396.     int w, int h
  397.     )
  398.     {
  399.     scn_putimage(ximage->width,ximage->height,(unsigned char *) ximage->data);
  400.     }
  401. /*...e*/
  402. /*...e*/
  403.